package com.atguigu.lease.web.admin.tasks;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyTasks {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0/20 * * * * *")
    public void checkMLease(){
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper
                = new LambdaUpdateWrapper<>();
        //判断要检查那种类型的状态
        lambdaUpdateWrapper.in(LeaseAgreement::getStatus,
                LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        //判断到期时间和当前时间的大小
        lambdaUpdateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date());
        //修改状态
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);

        leaseAgreementService.update(lambdaUpdateWrapper);
    }
}
